<?php /* УТФ-8:fix */

class SPImodel__catalogs__front extends SPImodel__catalogs
{	
	public function select( $tmode = null, $pid = 0, $filter_search = '', $orderby = null )
    {
		if( $pid !== 'all' )
        {
            $pid = (int) $pid;
        }

        switch( $orderby['dir'] )
        {
            default:
            case '+':   $orderby['dir'] = 'ASC';  break;
            case '-':   $orderby['dir'] = 'DESC'; break;
        }

        switch( $orderby['field'] )
        {
            default:
            case 'order_index': $q_orderby = "order_index {$orderby['dir']}, id ASC"; break;
            case 'name':        $q_orderby = "name {$orderby['dir']}, id ASC"; break;
            case 'id':          $q_orderby = "id {$orderby['dir']}"; break;
			case 'type':        $q_orderby = "type {$orderby['dir']}, id ASC"; break;
        }

        $store = SPIstore::I();

        $query =    " SELECT id "
               .      " FROM ".SPIcfg::$store['table:catalogs'] 
               .     " WHERE workstatus = 1 "
               . ( $tmode ? " AND type = '".addslashes($tmode)."' " : "" )
               . ( $pid === 'all' ? " AND TRUE " : ( $pid == 0 ? " AND pid IS NULL " : " AND pid = {$pid} " ) )
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'name', $filter_search )
				 . '         OR ' . $store->prepare_search_query( 'type', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY {$q_orderby} "
               ;
		$qresult = $store->query( $query );
		$res = array();
		while ($item = $qresult-> fetch()){
			$res[] = $item['id'];
		}
		$qresult->close();
		
        return $res;
    }

    public function get_by_id( $item_id )
    {	
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:catalogs']
               .     " WHERE workstatus=1 "
               .       " AND id=" . (int) $item_id
               ;

        $res = SPIstore::I()->query( $query )->fetch_and_close();

        return $res;
    }
}
